package com.custom.action.core.methods.select;

import com.custom.action.core.methods.AbstractMethod;
import com.custom.action.core.methods.MethodKind;
import com.custom.jdbc.executebody.ExecuteBodyHelper;
import com.custom.jdbc.executebody.SelectMapExecutorBody;
import com.custom.jdbc.interfaces.CustomSqlSession;
import com.custom.jdbc.session.JdbcSqlSessionFactory;

/**
 * @author Xiao-Bai
 * @since 2023/3/11 22:37
 */
public class SelectMap extends AbstractMethod {

    @Override
    protected <T> CustomSqlSession createSqlSession(JdbcSqlSessionFactory sqlSessionFactory, Class<T> target, Object[] params) throws Exception {
        return null;
    }

    private SelectMapExecutorBody createMapBody(Object[] params) {
        return ExecuteBodyHelper.createSelectMap(
                getMappedType(params, 0), getMappedType(params, 1),
                String.valueOf(params[2]), sqlPrintSupport, (Object[]) params[3]
        );
    }

    @Override
    public <T> Object doExecute(JdbcSqlSessionFactory sqlSessionFactory, Class<T> target, Object[] params) throws Exception {
        SelectMapExecutorBody mapExecutorBody = createMapBody(params);
        CustomSqlSession sqlSession = sqlSessionFactory.createSqlSession(mapExecutorBody);
        return sqlSessionFactory.getJdbcExecutor().selectMap(sqlSession);
    }

    @Override
    public MethodKind getKind() {
        return MethodKind.SELECT_MAP;
    }
}
